3 * Representation of a page version.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\Linker\LinkTarget
;
27 class Revision
implements IDBAccessObject
{
35 protected $mOrigUserText;
39 protected $mMinorEdit;
41 protected $mTimestamp;
57 protected $mUnpatrolled;
59 /** @var stdClass|null */
62 /** @var null|Title */
67 protected $mContentModel;
69 protected $mContentFormat;
71 /** @var Content|null|bool */
73 /** @var null|ContentHandler */
74 protected $mContentHandler;
77 protected $mQueryFlags = 0;
78 /** @var bool Used for cached values to reload user text and rev_deleted */
79 protected $mRefreshMutableFields = false;
80 /** @var string Wiki ID; false means the current wiki */
81 protected $mWiki = false;
83 // Revision deletion constants
84 const DELETED_TEXT
= 1;
85 const DELETED_COMMENT
= 2;
86 const DELETED_USER
= 4;
87 const DELETED_RESTRICTED
= 8;
88 const SUPPRESSED_USER
= 12; // convenience
90 // Audience options for accessors
92 const FOR_THIS_USER
= 2;
95 const TEXT_CACHE_GROUP
= 'revisiontext:10'; // process cache name and max key count
98 * Load a page revision from a given revision ID number.
99 * Returns null if no such revision can be found.
102 * Revision::READ_LATEST : Select the data from the master
103 * Revision::READ_LOCKING : Select & lock the data from the master
106 * @param int $flags (optional)
107 * @return Revision|null
109 public static function newFromId( $id, $flags = 0 ) {
110 return self
::newFromConds( [ 'rev_id' => intval( $id ) ], $flags );
114 * Load either the current, or a specified, revision
115 * that's attached to a given link target. If not attached
116 * to that link target, will return null.
119 * Revision::READ_LATEST : Select the data from the master
120 * Revision::READ_LOCKING : Select & lock the data from the master
122 * @param LinkTarget $linkTarget
123 * @param int $id (optional)
124 * @param int $flags Bitfield (optional)
125 * @return Revision|null
127 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
129 'page_namespace' => $linkTarget->getNamespace(),
130 'page_title' => $linkTarget->getDBkey()
133 // Use the specified ID
134 $conds['rev_id'] = $id;
135 return self
::newFromConds( $conds, $flags );
137 // Use a join to get the latest revision
138 $conds[] = 'rev_id=page_latest';
139 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
140 return self
::loadFromConds( $db, $conds, $flags );
145 * Load either the current, or a specified, revision
146 * that's attached to a given page ID.
147 * Returns null if no such revision can be found.
150 * Revision::READ_LATEST : Select the data from the master (since 1.20)
151 * Revision::READ_LOCKING : Select & lock the data from the master
154 * @param int $revId (optional)
155 * @param int $flags Bitfield (optional)
156 * @return Revision|null
158 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
159 $conds = [ 'page_id' => $pageId ];
161 $conds['rev_id'] = $revId;
162 return self
::newFromConds( $conds, $flags );
164 // Use a join to get the latest revision
165 $conds[] = 'rev_id = page_latest';
166 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
167 return self
::loadFromConds( $db, $conds, $flags );
172 * Make a fake revision object from an archive table row. This is queried
173 * for permissions or even inserted (as in Special:Undelete)
174 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
177 * @param array $overrides
179 * @throws MWException
182 public static function newFromArchiveRow( $row, $overrides = [] ) {
183 global $wgContentHandlerUseDB;
185 $attribs = $overrides +
[
186 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
187 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
188 'comment' => $row->ar_comment
,
189 'user' => $row->ar_user
,
190 'user_text' => $row->ar_user_text
,
191 'timestamp' => $row->ar_timestamp
,
192 'minor_edit' => $row->ar_minor_edit
,
193 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
194 'deleted' => $row->ar_deleted
,
195 'len' => $row->ar_len
,
196 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
197 'content_model' => isset( $row->ar_content_model
) ?
$row->ar_content_model
: null,
198 'content_format' => isset( $row->ar_content_format
) ?
$row->ar_content_format
: null,
201 if ( !$wgContentHandlerUseDB ) {
202 unset( $attribs['content_model'] );
203 unset( $attribs['content_format'] );
206 if ( !isset( $attribs['title'] )
207 && isset( $row->ar_namespace
)
208 && isset( $row->ar_title
)
210 $attribs['title'] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
213 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
214 // Pre-1.5 ar_text row
215 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
216 if ( $attribs['text'] === false ) {
217 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
220 return new self( $attribs );
229 public static function newFromRow( $row ) {
230 return new self( $row );
234 * Load a page revision from a given revision ID number.
235 * Returns null if no such revision can be found.
237 * @param IDatabase $db
239 * @return Revision|null
241 public static function loadFromId( $db, $id ) {
242 return self
::loadFromConds( $db, [ 'rev_id' => intval( $id ) ] );
246 * Load either the current, or a specified, revision
247 * that's attached to a given page. If not attached
248 * to that page, will return null.
250 * @param IDatabase $db
253 * @return Revision|null
255 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
256 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
258 $conds['rev_id'] = intval( $id );
260 $conds[] = 'rev_id=page_latest';
262 return self
::loadFromConds( $db, $conds );
266 * Load either the current, or a specified, revision
267 * that's attached to a given page. If not attached
268 * to that page, will return null.
270 * @param IDatabase $db
271 * @param Title $title
273 * @return Revision|null
275 public static function loadFromTitle( $db, $title, $id = 0 ) {
277 $matchId = intval( $id );
279 $matchId = 'page_latest';
281 return self
::loadFromConds( $db,
284 'page_namespace' => $title->getNamespace(),
285 'page_title' => $title->getDBkey()
291 * Load the revision for the given title with the given timestamp.
292 * WARNING: Timestamps may in some circumstances not be unique,
293 * so this isn't the best key to use.
295 * @param IDatabase $db
296 * @param Title $title
297 * @param string $timestamp
298 * @return Revision|null
300 public static function loadFromTimestamp( $db, $title, $timestamp ) {
301 return self
::loadFromConds( $db,
303 'rev_timestamp' => $db->timestamp( $timestamp ),
304 'page_namespace' => $title->getNamespace(),
305 'page_title' => $title->getDBkey()
311 * Given a set of conditions, fetch a revision
313 * This method is used then a revision ID is qualified and
314 * will incorporate some basic replica DB/master fallback logic
316 * @param array $conditions
317 * @param int $flags (optional)
318 * @return Revision|null
320 private static function newFromConds( $conditions, $flags = 0 ) {
321 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
323 $rev = self
::loadFromConds( $db, $conditions, $flags );
324 // Make sure new pending/committed revision are visibile later on
325 // within web requests to certain avoid bugs like T93866 and T94407.
327 && !( $flags & self
::READ_LATEST
)
328 && wfGetLB()->getServerCount() > 1
329 && wfGetLB()->hasOrMadeRecentMasterChanges()
331 $flags = self
::READ_LATEST
;
332 $db = wfGetDB( DB_MASTER
);
333 $rev = self
::loadFromConds( $db, $conditions, $flags );
337 $rev->mQueryFlags
= $flags;
344 * Given a set of conditions, fetch a revision from
345 * the given database connection.
347 * @param IDatabase $db
348 * @param array $conditions
349 * @param int $flags (optional)
350 * @return Revision|null
352 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
353 $row = self
::fetchFromConds( $db, $conditions, $flags );
355 $rev = new Revision( $row );
356 $rev->mWiki
= $db->getWikiID();
365 * Return a wrapper for a series of database rows to
366 * fetch all of a given page's revisions in turn.
367 * Each row can be fed to the constructor to get objects.
369 * @param LinkTarget $title
370 * @return ResultWrapper
371 * @deprecated Since 1.28
373 public static function fetchRevision( LinkTarget
$title ) {
374 $row = self
::fetchFromConds(
375 wfGetDB( DB_REPLICA
),
377 'rev_id=page_latest',
378 'page_namespace' => $title->getNamespace(),
379 'page_title' => $title->getDBkey()
383 return new FakeResultWrapper( $row ?
[ $row ] : [] );
387 * Given a set of conditions, return a ResultWrapper
388 * which will return matching database rows with the
389 * fields necessary to build Revision objects.
391 * @param IDatabase $db
392 * @param array $conditions
393 * @param int $flags (optional)
396 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
397 $fields = array_merge(
398 self
::selectFields(),
399 self
::selectPageFields(),
400 self
::selectUserFields()
403 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
404 $options[] = 'FOR UPDATE';
406 return $db->selectRow(
407 [ 'revision', 'page', 'user' ],
412 [ 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() ]
417 * Return the value of a select() JOIN conds array for the user table.
418 * This will get user table rows for logged-in users.
422 public static function userJoinCond() {
423 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
427 * Return the value of a select() page conds array for the page table.
428 * This will assure that the revision(s) are not orphaned from live pages.
432 public static function pageJoinCond() {
433 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
437 * Return the list of revision fields that should be selected to create
441 public static function selectFields() {
442 global $wgContentHandlerUseDB;
459 if ( $wgContentHandlerUseDB ) {
460 $fields[] = 'rev_content_format';
461 $fields[] = 'rev_content_model';
468 * Return the list of revision fields that should be selected to create
469 * a new revision from an archive row.
472 public static function selectArchiveFields() {
473 global $wgContentHandlerUseDB;
491 if ( $wgContentHandlerUseDB ) {
492 $fields[] = 'ar_content_format';
493 $fields[] = 'ar_content_model';
499 * Return the list of text fields that should be selected to read the
503 public static function selectTextFields() {
511 * Return the list of page fields that should be selected from page table
514 public static function selectPageFields() {
526 * Return the list of user fields that should be selected from user table
529 public static function selectUserFields() {
530 return [ 'user_name' ];
534 * Do a batched query to get the parent revision lengths
535 * @param IDatabase $db
536 * @param array $revIds
539 public static function getParentLengths( $db, array $revIds ) {
542 return $revLens; // empty
544 $res = $db->select( 'revision',
545 [ 'rev_id', 'rev_len' ],
546 [ 'rev_id' => $revIds ],
548 foreach ( $res as $row ) {
549 $revLens[$row->rev_id
] = $row->rev_len
;
557 * @param object|array $row Either a database row or an array
558 * @throws MWException
561 function __construct( $row ) {
562 if ( is_object( $row ) ) {
563 $this->mId
= intval( $row->rev_id
);
564 $this->mPage
= intval( $row->rev_page
);
565 $this->mTextId
= intval( $row->rev_text_id
);
566 $this->mComment
= $row->rev_comment
;
567 $this->mUser
= intval( $row->rev_user
);
568 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
569 $this->mTimestamp
= $row->rev_timestamp
;
570 $this->mDeleted
= intval( $row->rev_deleted
);
572 if ( !isset( $row->rev_parent_id
) ) {
573 $this->mParentId
= null;
575 $this->mParentId
= intval( $row->rev_parent_id
);
578 if ( !isset( $row->rev_len
) ) {
581 $this->mSize
= intval( $row->rev_len
);
584 if ( !isset( $row->rev_sha1
) ) {
587 $this->mSha1
= $row->rev_sha1
;
590 if ( isset( $row->page_latest
) ) {
591 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
592 $this->mTitle
= Title
::newFromRow( $row );
594 $this->mCurrent
= false;
595 $this->mTitle
= null;
598 if ( !isset( $row->rev_content_model
) ) {
599 $this->mContentModel
= null; # determine on demand if needed
601 $this->mContentModel
= strval( $row->rev_content_model
);
604 if ( !isset( $row->rev_content_format
) ) {
605 $this->mContentFormat
= null; # determine on demand if needed
607 $this->mContentFormat
= strval( $row->rev_content_format
);
610 // Lazy extraction...
612 if ( isset( $row->old_text
) ) {
613 $this->mTextRow
= $row;
615 // 'text' table row entry will be lazy-loaded
616 $this->mTextRow
= null;
619 // Use user_name for users and rev_user_text for IPs...
620 $this->mUserText
= null; // lazy load if left null
621 if ( $this->mUser
== 0 ) {
622 $this->mUserText
= $row->rev_user_text
; // IP user
623 } elseif ( isset( $row->user_name
) ) {
624 $this->mUserText
= $row->user_name
; // logged-in user
626 $this->mOrigUserText
= $row->rev_user_text
;
627 } elseif ( is_array( $row ) ) {
628 // Build a new revision to be saved...
629 global $wgUser; // ugh
631 # if we have a content object, use it to set the model and type
632 if ( !empty( $row['content'] ) ) {
633 // @todo when is that set? test with external store setup! check out insertOn() [dk]
634 if ( !empty( $row['text_id'] ) ) {
635 throw new MWException( "Text already stored in external store (id {$row['text_id']}), " .
636 "can't serialize content object" );
639 $row['content_model'] = $row['content']->getModel();
640 # note: mContentFormat is initializes later accordingly
641 # note: content is serialized later in this method!
642 # also set text to null?
645 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
646 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
647 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
648 $this->mUserText
= isset( $row['user_text'] )
649 ?
strval( $row['user_text'] ) : $wgUser->getName();
650 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
651 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
652 $this->mTimestamp
= isset( $row['timestamp'] )
653 ?
strval( $row['timestamp'] ) : wfTimestampNow();
654 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
655 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
656 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
657 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
659 $this->mContentModel
= isset( $row['content_model'] )
660 ?
strval( $row['content_model'] ) : null;
661 $this->mContentFormat
= isset( $row['content_format'] )
662 ?
strval( $row['content_format'] ) : null;
664 // Enforce spacing trimming on supplied text
665 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
666 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
667 $this->mTextRow
= null;
669 $this->mTitle
= isset( $row['title'] ) ?
$row['title'] : null;
671 // if we have a Content object, override mText and mContentModel
672 if ( !empty( $row['content'] ) ) {
673 if ( !( $row['content'] instanceof Content
) ) {
674 throw new MWException( '`content` field must contain a Content object.' );
677 $handler = $this->getContentHandler();
678 $this->mContent
= $row['content'];
680 $this->mContentModel
= $this->mContent
->getModel();
681 $this->mContentHandler
= null;
683 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
684 } elseif ( $this->mText
!== null ) {
685 $handler = $this->getContentHandler();
686 $this->mContent
= $handler->unserializeContent( $this->mText
);
689 // If we have a Title object, make sure it is consistent with mPage.
690 if ( $this->mTitle
&& $this->mTitle
->exists() ) {
691 if ( $this->mPage
=== null ) {
692 // if the page ID wasn't known, set it now
693 $this->mPage
= $this->mTitle
->getArticleID();
694 } elseif ( $this->mTitle
->getArticleID() !== $this->mPage
) {
695 // Got different page IDs. This may be legit (e.g. during undeletion),
696 // but it seems worth mentioning it in the log.
697 wfDebug( "Page ID " . $this->mPage
. " mismatches the ID " .
698 $this->mTitle
->getArticleID() . " provided by the Title object." );
702 $this->mCurrent
= false;
704 // If we still have no length, see it we have the text to figure it out
705 if ( !$this->mSize
&& $this->mContent
!== null ) {
706 $this->mSize
= $this->mContent
->getSize();
710 if ( $this->mSha1
=== null ) {
711 $this->mSha1
= $this->mText
=== null ?
null : self
::base36Sha1( $this->mText
);
715 $this->getContentModel();
716 $this->getContentFormat();
718 throw new MWException( 'Revision constructor passed invalid row format.' );
720 $this->mUnpatrolled
= null;
728 public function getId() {
733 * Set the revision ID
735 * This should only be used for proposed revisions that turn out to be null edits
740 public function setId( $id ) {
741 $this->mId
= (int)$id;
745 * Set the user ID/name
747 * This should only be used for proposed revisions that turn out to be null edits
750 * @param integer $id User ID
751 * @param string $name User name
753 public function setUserIdAndName( $id, $name ) {
754 $this->mUser
= (int)$id;
755 $this->mUserText
= $name;
756 $this->mOrigUserText
= $name;
764 public function getTextId() {
765 return $this->mTextId
;
769 * Get parent revision ID (the original previous page revision)
773 public function getParentId() {
774 return $this->mParentId
;
778 * Returns the length of the text in this revision, or null if unknown.
782 public function getSize() {
787 * Returns the base36 sha1 of the text in this revision, or null if unknown.
789 * @return string|null
791 public function getSha1() {
796 * Returns the title of the page associated with this entry or null.
798 * Will do a query, when title is not set and id is given.
802 public function getTitle() {
803 if ( $this->mTitle
!== null ) {
804 return $this->mTitle
;
806 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
807 if ( $this->mId
!== null ) {
808 $dbr = wfGetLB( $this->mWiki
)->getConnectionRef( DB_REPLICA
, [], $this->mWiki
);
809 $row = $dbr->selectRow(
810 [ 'page', 'revision' ],
811 self
::selectPageFields(),
812 [ 'page_id=rev_page', 'rev_id' => $this->mId
],
816 // @TODO: better foreign title handling
817 $this->mTitle
= Title
::newFromRow( $row );
821 if ( $this->mWiki
=== false ||
$this->mWiki
=== wfWikiID() ) {
822 // Loading by ID is best, though not possible for foreign titles
823 if ( !$this->mTitle
&& $this->mPage
!== null && $this->mPage
> 0 ) {
824 $this->mTitle
= Title
::newFromID( $this->mPage
);
828 return $this->mTitle
;
832 * Set the title of the revision
834 * @param Title $title
836 public function setTitle( $title ) {
837 $this->mTitle
= $title;
845 public function getPage() {
850 * Fetch revision's user id if it's available to the specified audience.
851 * If the specified audience does not have access to it, zero will be
854 * @param int $audience One of:
855 * Revision::FOR_PUBLIC to be displayed to all users
856 * Revision::FOR_THIS_USER to be displayed to the given user
857 * Revision::RAW get the ID regardless of permissions
858 * @param User $user User object to check for, only if FOR_THIS_USER is passed
859 * to the $audience parameter
862 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
863 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
865 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
873 * Fetch revision's user id without regard for the current user's permissions
876 * @deprecated since 1.25, use getUser( Revision::RAW )
878 public function getRawUser() {
879 wfDeprecated( __METHOD__
, '1.25' );
880 return $this->getUser( self
::RAW
);
884 * Fetch revision's username if it's available to the specified audience.
885 * If the specified audience does not have access to the username, an
886 * empty string will be returned.
888 * @param int $audience One of:
889 * Revision::FOR_PUBLIC to be displayed to all users
890 * Revision::FOR_THIS_USER to be displayed to the given user
891 * Revision::RAW get the text regardless of permissions
892 * @param User $user User object to check for, only if FOR_THIS_USER is passed
893 * to the $audience parameter
896 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
897 $this->loadMutableFields();
899 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
901 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
904 if ( $this->mUserText
=== null ) {
905 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
906 if ( $this->mUserText
=== false ) {
907 # This shouldn't happen, but it can if the wiki was recovered
908 # via importing revs and there is no user table entry yet.
909 $this->mUserText
= $this->mOrigUserText
;
912 return $this->mUserText
;
917 * Fetch revision's username without regard for view restrictions
920 * @deprecated since 1.25, use getUserText( Revision::RAW )
922 public function getRawUserText() {
923 wfDeprecated( __METHOD__
, '1.25' );
924 return $this->getUserText( self
::RAW
);
928 * Fetch revision comment if it's available to the specified audience.
929 * If the specified audience does not have access to the comment, an
930 * empty string will be returned.
932 * @param int $audience One of:
933 * Revision::FOR_PUBLIC to be displayed to all users
934 * Revision::FOR_THIS_USER to be displayed to the given user
935 * Revision::RAW get the text regardless of permissions
936 * @param User $user User object to check for, only if FOR_THIS_USER is passed
937 * to the $audience parameter
940 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
941 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
943 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
946 return $this->mComment
;
951 * Fetch revision comment without regard for the current user's permissions
954 * @deprecated since 1.25, use getComment( Revision::RAW )
956 public function getRawComment() {
957 wfDeprecated( __METHOD__
, '1.25' );
958 return $this->getComment( self
::RAW
);
964 public function isMinor() {
965 return (bool)$this->mMinorEdit
;
969 * @return int Rcid of the unpatrolled row, zero if there isn't one
971 public function isUnpatrolled() {
972 if ( $this->mUnpatrolled
!== null ) {
973 return $this->mUnpatrolled
;
975 $rc = $this->getRecentChange();
976 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
977 $this->mUnpatrolled
= $rc->getAttribute( 'rc_id' );
979 $this->mUnpatrolled
= 0;
981 return $this->mUnpatrolled
;
985 * Get the RC object belonging to the current revision, if there's one
987 * @param int $flags (optional) $flags include:
988 * Revision::READ_LATEST : Select the data from the master
991 * @return RecentChange|null
993 public function getRecentChange( $flags = 0 ) {
994 $dbr = wfGetDB( DB_REPLICA
);
996 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
998 return RecentChange
::newFromConds(
1000 'rc_user_text' => $this->getUserText( Revision
::RAW
),
1001 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
1002 'rc_this_oldid' => $this->getId()
1010 * @param int $field One of DELETED_* bitfield constants
1014 public function isDeleted( $field ) {
1015 if ( $this->isCurrent() && $field === self
::DELETED_TEXT
) {
1016 // Current revisions of pages cannot have the content hidden. Skipping this
1017 // check is very useful for Parser as it fetches templates using newKnownCurrent().
1018 // Calling getVisibility() in that case triggers a verification database query.
1019 return false; // no need to check
1022 return ( $this->getVisibility() & $field ) == $field;
1026 * Get the deletion bitfield of the revision
1030 public function getVisibility() {
1031 $this->loadMutableFields();
1033 return (int)$this->mDeleted
;
1037 * Fetch revision text if it's available to the specified audience.
1038 * If the specified audience does not have the ability to view this
1039 * revision, an empty string will be returned.
1041 * @param int $audience One of:
1042 * Revision::FOR_PUBLIC to be displayed to all users
1043 * Revision::FOR_THIS_USER to be displayed to the given user
1044 * Revision::RAW get the text regardless of permissions
1045 * @param User $user User object to check for, only if FOR_THIS_USER is passed
1046 * to the $audience parameter
1048 * @deprecated since 1.21, use getContent() instead
1049 * @todo Replace usage in core
1052 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
1053 ContentHandler
::deprecated( __METHOD__
, '1.21' );
1055 $content = $this->getContent( $audience, $user );
1056 return ContentHandler
::getContentText( $content ); # returns the raw content text, if applicable
1060 * Fetch revision content if it's available to the specified audience.
1061 * If the specified audience does not have the ability to view this
1062 * revision, null will be returned.
1064 * @param int $audience One of:
1065 * Revision::FOR_PUBLIC to be displayed to all users
1066 * Revision::FOR_THIS_USER to be displayed to $wgUser
1067 * Revision::RAW get the text regardless of permissions
1068 * @param User $user User object to check for, only if FOR_THIS_USER is passed
1069 * to the $audience parameter
1071 * @return Content|null
1073 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
1074 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
1076 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
1079 return $this->getContentInternal();
1084 * Get original serialized data (without checking view restrictions)
1089 public function getSerializedData() {
1090 if ( $this->mText
=== null ) {
1091 // Revision is immutable. Load on demand.
1092 $this->mText
= $this->loadText();
1095 return $this->mText
;
1099 * Gets the content object for the revision (or null on failure).
1101 * Note that for mutable Content objects, each call to this method will return a
1105 * @return Content|null The Revision's content, or null on failure.
1107 protected function getContentInternal() {
1108 if ( $this->mContent
=== null ) {
1109 $text = $this->getSerializedData();
1111 if ( $text !== null && $text !== false ) {
1112 // Unserialize content
1113 $handler = $this->getContentHandler();
1114 $format = $this->getContentFormat();
1116 $this->mContent
= $handler->unserializeContent( $text, $format );
1120 // NOTE: copy() will return $this for immutable content objects
1121 return $this->mContent ?
$this->mContent
->copy() : null;
1125 * Returns the content model for this revision.
1127 * If no content model was stored in the database, the default content model for the title is
1128 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1129 * is used as a last resort.
1131 * @return string The content model id associated with this revision,
1132 * see the CONTENT_MODEL_XXX constants.
1134 public function getContentModel() {
1135 if ( !$this->mContentModel
) {
1136 $title = $this->getTitle();
1138 $this->mContentModel
= ContentHandler
::getDefaultModelFor( $title );
1140 $this->mContentModel
= CONTENT_MODEL_WIKITEXT
;
1143 assert( !empty( $this->mContentModel
) );
1146 return $this->mContentModel
;
1150 * Returns the content format for this revision.
1152 * If no content format was stored in the database, the default format for this
1153 * revision's content model is returned.
1155 * @return string The content format id associated with this revision,
1156 * see the CONTENT_FORMAT_XXX constants.
1158 public function getContentFormat() {
1159 if ( !$this->mContentFormat
) {
1160 $handler = $this->getContentHandler();
1161 $this->mContentFormat
= $handler->getDefaultFormat();
1163 assert( !empty( $this->mContentFormat
) );
1166 return $this->mContentFormat
;
1170 * Returns the content handler appropriate for this revision's content model.
1172 * @throws MWException
1173 * @return ContentHandler
1175 public function getContentHandler() {
1176 if ( !$this->mContentHandler
) {
1177 $model = $this->getContentModel();
1178 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
1180 $format = $this->getContentFormat();
1182 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
1183 throw new MWException( "Oops, the content format $format is not supported for "
1184 . "this content model, $model" );
1188 return $this->mContentHandler
;
1194 public function getTimestamp() {
1195 return wfTimestamp( TS_MW
, $this->mTimestamp
);
1201 public function isCurrent() {
1202 return $this->mCurrent
;
1206 * Get previous revision for this title
1208 * @return Revision|null
1210 public function getPrevious() {
1211 if ( $this->getTitle() ) {
1212 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1214 return self
::newFromTitle( $this->getTitle(), $prev );
1221 * Get next revision for this title
1223 * @return Revision|null
1225 public function getNext() {
1226 if ( $this->getTitle() ) {
1227 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1229 return self
::newFromTitle( $this->getTitle(), $next );
1236 * Get previous revision Id for this page_id
1237 * This is used to populate rev_parent_id on save
1239 * @param IDatabase $db
1242 private function getPreviousRevisionId( $db ) {
1243 if ( $this->mPage
=== null ) {
1246 # Use page_latest if ID is not given
1247 if ( !$this->mId
) {
1248 $prevId = $db->selectField( 'page', 'page_latest',
1249 [ 'page_id' => $this->mPage
],
1252 $prevId = $db->selectField( 'revision', 'rev_id',
1253 [ 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
],
1255 [ 'ORDER BY' => 'rev_id DESC' ] );
1257 return intval( $prevId );
1261 * Get revision text associated with an old or archive row
1262 * $row is usually an object from wfFetchRow(), both the flags and the text
1263 * field must be included.
1265 * @param stdClass $row The text data
1266 * @param string $prefix Table prefix (default 'old_')
1267 * @param string|bool $wiki The name of the wiki to load the revision text from
1268 * (same as the the wiki $row was loaded from) or false to indicate the local
1269 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1270 * identifier as understood by the LoadBalancer class.
1271 * @return string Text the text requested or false on failure
1273 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1276 $textField = $prefix . 'text';
1277 $flagsField = $prefix . 'flags';
1279 if ( isset( $row->$flagsField ) ) {
1280 $flags = explode( ',', $row->$flagsField );
1285 if ( isset( $row->$textField ) ) {
1286 $text = $row->$textField;
1291 # Use external methods for external objects, text in table is URL-only then
1292 if ( in_array( 'external', $flags ) ) {
1294 $parts = explode( '://', $url, 2 );
1295 if ( count( $parts ) == 1 ||
$parts[1] == '' ) {
1298 $text = ExternalStore
::fetchFromURL( $url, [ 'wiki' => $wiki ] );
1301 // If the text was fetched without an error, convert it
1302 if ( $text !== false ) {
1303 $text = self
::decompressRevisionText( $text, $flags );
1309 * If $wgCompressRevisions is enabled, we will compress data.
1310 * The input string is modified in place.
1311 * Return value is the flags field: contains 'gzip' if the
1312 * data is compressed, and 'utf-8' if we're saving in UTF-8
1315 * @param mixed $text Reference to a text
1318 public static function compressRevisionText( &$text ) {
1319 global $wgCompressRevisions;
1322 # Revisions not marked this way will be converted
1323 # on load if $wgLegacyCharset is set in the future.
1326 if ( $wgCompressRevisions ) {
1327 if ( function_exists( 'gzdeflate' ) ) {
1328 $deflated = gzdeflate( $text );
1330 if ( $deflated === false ) {
1331 wfLogWarning( __METHOD__
. ': gzdeflate() failed' );
1337 wfDebug( __METHOD__
. " -- no zlib support, not compressing\n" );
1340 return implode( ',', $flags );
1344 * Re-converts revision text according to it's flags.
1346 * @param mixed $text Reference to a text
1347 * @param array $flags Compression flags
1348 * @return string|bool Decompressed text, or false on failure
1350 public static function decompressRevisionText( $text, $flags ) {
1351 if ( in_array( 'gzip', $flags ) ) {
1352 # Deal with optional compression of archived pages.
1353 # This can be done periodically via maintenance/compressOld.php, and
1354 # as pages are saved if $wgCompressRevisions is set.
1355 $text = gzinflate( $text );
1357 if ( $text === false ) {
1358 wfLogWarning( __METHOD__
. ': gzinflate() failed' );
1363 if ( in_array( 'object', $flags ) ) {
1364 # Generic compressed storage
1365 $obj = unserialize( $text );
1366 if ( !is_object( $obj ) ) {
1370 $text = $obj->getText();
1373 global $wgLegacyEncoding;
1374 if ( $text !== false && $wgLegacyEncoding
1375 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags )
1377 # Old revisions kept around in a legacy encoding?
1378 # Upconvert on demand.
1379 # ("utf8" checked for compatibility with some broken
1380 # conversion scripts 2008-12-30)
1382 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1389 * Insert a new revision into the database, returning the new revision ID
1390 * number on success and dies horribly on failure.
1392 * @param IDatabase $dbw (master connection)
1393 * @throws MWException
1396 public function insertOn( $dbw ) {
1397 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1399 // Not allowed to have rev_page equal to 0, false, etc.
1400 if ( !$this->mPage
) {
1401 $title = $this->getTitle();
1402 if ( $title instanceof Title
) {
1403 $titleText = ' for page ' . $title->getPrefixedText();
1407 throw new MWException( "Cannot insert revision$titleText: page ID must be nonzero" );
1410 $this->checkContentModel();
1412 $data = $this->mText
;
1413 $flags = self
::compressRevisionText( $data );
1415 # Write to external storage if required
1416 if ( $wgDefaultExternalStore ) {
1417 // Store and get the URL
1418 $data = ExternalStore
::insertToDefault( $data );
1420 throw new MWException( "Unable to store text to external storage" );
1425 $flags .= 'external';
1428 # Record the text (or external storage URL) to the text table
1429 if ( $this->mTextId
=== null ) {
1430 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1431 $dbw->insert( 'text',
1433 'old_id' => $old_id,
1434 'old_text' => $data,
1435 'old_flags' => $flags,
1438 $this->mTextId
= $dbw->insertId();
1441 if ( $this->mComment
=== null ) {
1442 $this->mComment
= "";
1445 # Record the edit in revisions
1446 $rev_id = $this->mId
!== null
1448 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1450 'rev_id' => $rev_id,
1451 'rev_page' => $this->mPage
,
1452 'rev_text_id' => $this->mTextId
,
1453 'rev_comment' => $this->mComment
,
1454 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1455 'rev_user' => $this->mUser
,
1456 'rev_user_text' => $this->mUserText
,
1457 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1458 'rev_deleted' => $this->mDeleted
,
1459 'rev_len' => $this->mSize
,
1460 'rev_parent_id' => $this->mParentId
=== null
1461 ?
$this->getPreviousRevisionId( $dbw )
1463 'rev_sha1' => $this->mSha1
=== null
1464 ? Revision
::base36Sha1( $this->mText
)
1468 if ( $wgContentHandlerUseDB ) {
1469 // NOTE: Store null for the default model and format, to save space.
1470 // XXX: Makes the DB sensitive to changed defaults.
1471 // Make this behavior optional? Only in miser mode?
1473 $model = $this->getContentModel();
1474 $format = $this->getContentFormat();
1476 $title = $this->getTitle();
1478 if ( $title === null ) {
1479 throw new MWException( "Insufficient information to determine the title of the "
1480 . "revision's page!" );
1483 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1484 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
1486 $row['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
1487 $row['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
1490 $dbw->insert( 'revision', $row, __METHOD__
);
1492 $this->mId
= $rev_id !== null ?
$rev_id : $dbw->insertId();
1494 // Assertion to try to catch T92046
1495 if ( (int)$this->mId
=== 0 ) {
1496 throw new UnexpectedValueException(
1497 'After insert, Revision mId is ' . var_export( $this->mId
, 1 ) . ': ' .
1498 var_export( $row, 1 )
1502 Hooks
::run( 'RevisionInsertComplete', [ &$this, $data, $flags ] );
1507 protected function checkContentModel() {
1508 global $wgContentHandlerUseDB;
1510 // Note: may return null for revisions that have not yet been inserted
1511 $title = $this->getTitle();
1513 $model = $this->getContentModel();
1514 $format = $this->getContentFormat();
1515 $handler = $this->getContentHandler();
1517 if ( !$handler->isSupportedFormat( $format ) ) {
1518 $t = $title->getPrefixedDBkey();
1520 throw new MWException( "Can't use format $format with content model $model on $t" );
1523 if ( !$wgContentHandlerUseDB && $title ) {
1524 // if $wgContentHandlerUseDB is not set,
1525 // all revisions must use the default content model and format.
1527 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1528 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1529 $defaultFormat = $defaultHandler->getDefaultFormat();
1531 if ( $this->getContentModel() != $defaultModel ) {
1532 $t = $title->getPrefixedDBkey();
1534 throw new MWException( "Can't save non-default content model with "
1535 . "\$wgContentHandlerUseDB disabled: model is $model, "
1536 . "default for $t is $defaultModel" );
1539 if ( $this->getContentFormat() != $defaultFormat ) {
1540 $t = $title->getPrefixedDBkey();
1542 throw new MWException( "Can't use non-default content format with "
1543 . "\$wgContentHandlerUseDB disabled: format is $format, "
1544 . "default for $t is $defaultFormat" );
1548 $content = $this->getContent( Revision
::RAW
);
1549 $prefixedDBkey = $title->getPrefixedDBkey();
1550 $revId = $this->mId
;
1553 throw new MWException(
1554 "Content of revision $revId ($prefixedDBkey) could not be loaded for validation!"
1557 if ( !$content->isValid() ) {
1558 throw new MWException(
1559 "Content of revision $revId ($prefixedDBkey) is not valid! Content model is $model"
1565 * Get the base 36 SHA-1 value for a string of text
1566 * @param string $text
1569 public static function base36Sha1( $text ) {
1570 return Wikimedia\base_convert
( sha1( $text ), 16, 36, 31 );
1574 * Lazy-load the revision's text.
1575 * Currently hardcoded to the 'text' table storage engine.
1577 * @return string|bool The revision's text, or false on failure
1579 private function loadText() {
1580 global $wgRevisionCacheExpiry;
1582 $cache = ObjectCache
::getMainWANInstance();
1583 if ( $cache->getQoS( $cache::ATTR_EMULATION
) <= $cache::QOS_EMULATION_SQL
) {
1584 // Do not cache RDBMs blobs in...the RDBMs store
1585 $ttl = $cache::TTL_UNCACHEABLE
;
1587 $ttl = $wgRevisionCacheExpiry ?
: $cache::TTL_UNCACHEABLE
;
1590 // No negative caching; negative hits on text rows may be due to corrupted replica DBs
1591 return $cache->getWithSetCallback(
1592 $cache->makeKey( 'revisiontext', 'textid', $this->getTextId() ),
1595 return $this->fetchText();
1597 [ 'pcGroup' => self
::TEXT_CACHE_GROUP
, 'pcTTL' => $cache::TTL_PROC_LONG
]
1601 private function fetchText() {
1602 $textId = $this->getTextId();
1604 // If we kept data for lazy extraction, use it now...
1605 if ( $this->mTextRow
!== null ) {
1606 $row = $this->mTextRow
;
1607 $this->mTextRow
= null;
1612 // Callers doing updates will pass in READ_LATEST as usual. Since the text/blob tables
1613 // do not normally get rows changed around, set READ_LATEST_IMMUTABLE in those cases.
1614 $flags = $this->mQueryFlags
;
1615 $flags |
= DBAccessObjectUtils
::hasFlags( $flags, self
::READ_LATEST
)
1616 ? self
::READ_LATEST_IMMUTABLE
1619 list( $index, $options, $fallbackIndex, $fallbackOptions ) =
1620 DBAccessObjectUtils
::getDBOptions( $flags );
1623 // Text data is immutable; check replica DBs first.
1624 $row = wfGetDB( $index )->selectRow(
1626 [ 'old_text', 'old_flags' ],
1627 [ 'old_id' => $textId ],
1633 // Fallback to DB_MASTER in some cases if the row was not found
1634 if ( !$row && $fallbackIndex !== null ) {
1635 // Use FOR UPDATE if it was used to fetch this revision. This avoids missing the row
1636 // due to REPEATABLE-READ. Also fallback to the master if READ_LATEST is provided.
1637 $row = wfGetDB( $fallbackIndex )->selectRow(
1639 [ 'old_text', 'old_flags' ],
1640 [ 'old_id' => $textId ],
1647 wfDebugLog( 'Revision', "No text row with ID '$textId' (revision {$this->getId()})." );
1650 $text = self
::getRevisionText( $row );
1651 if ( $row && $text === false ) {
1652 wfDebugLog( 'Revision', "No blob for text row '$textId' (revision {$this->getId()})." );
1655 return is_string( $text ) ?
$text : false;
1659 * Create a new null-revision for insertion into a page's
1660 * history. This will not re-save the text, but simply refer
1661 * to the text from the previous version.
1663 * Such revisions can for instance identify page rename
1664 * operations and other such meta-modifications.
1666 * @param IDatabase $dbw
1667 * @param int $pageId ID number of the page to read from
1668 * @param string $summary Revision's summary
1669 * @param bool $minor Whether the revision should be considered as minor
1670 * @param User|null $user User object to use or null for $wgUser
1671 * @return Revision|null Revision or null on error
1673 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1674 global $wgContentHandlerUseDB, $wgContLang;
1676 $fields = [ 'page_latest', 'page_namespace', 'page_title',
1677 'rev_text_id', 'rev_len', 'rev_sha1' ];
1679 if ( $wgContentHandlerUseDB ) {
1680 $fields[] = 'rev_content_model';
1681 $fields[] = 'rev_content_format';
1684 $current = $dbw->selectRow(
1685 [ 'page', 'revision' ],
1688 'page_id' => $pageId,
1689 'page_latest=rev_id',
1692 [ 'FOR UPDATE' ] // T51581
1701 // Truncate for whole multibyte characters
1702 $summary = $wgContLang->truncate( $summary, 255 );
1706 'user_text' => $user->getName(),
1707 'user' => $user->getId(),
1708 'comment' => $summary,
1709 'minor_edit' => $minor,
1710 'text_id' => $current->rev_text_id
,
1711 'parent_id' => $current->page_latest
,
1712 'len' => $current->rev_len
,
1713 'sha1' => $current->rev_sha1
1716 if ( $wgContentHandlerUseDB ) {
1717 $row['content_model'] = $current->rev_content_model
;
1718 $row['content_format'] = $current->rev_content_format
;
1721 $row['title'] = Title
::makeTitle( $current->page_namespace
, $current->page_title
);
1723 $revision = new Revision( $row );
1732 * Determine if the current user is allowed to view a particular
1733 * field of this revision, if it's marked as deleted.
1735 * @param int $field One of self::DELETED_TEXT,
1736 * self::DELETED_COMMENT,
1737 * self::DELETED_USER
1738 * @param User|null $user User object to check, or null to use $wgUser
1741 public function userCan( $field, User
$user = null ) {
1742 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1746 * Determine if the current user is allowed to view a particular
1747 * field of this revision, if it's marked as deleted. This is used
1748 * by various classes to avoid duplication.
1750 * @param int $bitfield Current field
1751 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1752 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1753 * self::DELETED_USER = File::DELETED_USER
1754 * @param User|null $user User object to check, or null to use $wgUser
1755 * @param Title|null $title A Title object to check for per-page restrictions on,
1756 * instead of just plain userrights
1759 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1762 if ( $bitfield & $field ) { // aspect is deleted
1763 if ( $user === null ) {
1767 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1768 $permissions = [ 'suppressrevision', 'viewsuppressed' ];
1769 } elseif ( $field & self
::DELETED_TEXT
) {
1770 $permissions = [ 'deletedtext' ];
1772 $permissions = [ 'deletedhistory' ];
1774 $permissionlist = implode( ', ', $permissions );
1775 if ( $title === null ) {
1776 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
1777 return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions );
1779 $text = $title->getPrefixedText();
1780 wfDebug( "Checking for $permissionlist on $text due to $field match on $bitfield\n" );
1781 foreach ( $permissions as $perm ) {
1782 if ( $title->userCan( $perm, $user ) ) {
1794 * Get rev_timestamp from rev_id, without loading the rest of the row
1796 * @param Title $title
1798 * @return string|bool False if not found
1800 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1801 $db = ( $flags & self
::READ_LATEST
)
1802 ?
wfGetDB( DB_MASTER
)
1803 : wfGetDB( DB_REPLICA
);
1804 // Casting fix for databases that can't take '' for rev_id
1808 $conds = [ 'rev_id' => $id ];
1809 $conds['rev_page'] = $title->getArticleID();
1810 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1812 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
1816 * Get count of revisions per page...not very efficient
1818 * @param IDatabase $db
1819 * @param int $id Page id
1822 static function countByPageId( $db, $id ) {
1823 $row = $db->selectRow( 'revision', [ 'revCount' => 'COUNT(*)' ],
1824 [ 'rev_page' => $id ], __METHOD__
);
1826 return $row->revCount
;
1832 * Get count of revisions per page...not very efficient
1834 * @param IDatabase $db
1835 * @param Title $title
1838 static function countByTitle( $db, $title ) {
1839 $id = $title->getArticleID();
1841 return self
::countByPageId( $db, $id );
1847 * Check if no edits were made by other users since
1848 * the time a user started editing the page. Limit to
1849 * 50 revisions for the sake of performance.
1852 * @deprecated since 1.24
1854 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1855 * Database object or a database identifier usable with wfGetDB.
1856 * @param int $pageId The ID of the page in question
1857 * @param int $userId The ID of the user in question
1858 * @param string $since Look at edits since this time
1860 * @return bool True if the given user was the only one to edit since the given timestamp
1862 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1867 if ( is_int( $db ) ) {
1868 $db = wfGetDB( $db );
1871 $res = $db->select( 'revision',
1874 'rev_page' => $pageId,
1875 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1878 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ] );
1879 foreach ( $res as $row ) {
1880 if ( $row->rev_user
!= $userId ) {
1888 * Load a revision based on a known page ID and current revision ID from the DB
1890 * This method allows for the use of caching, though accessing anything that normally
1891 * requires permission checks (aside from the text) will trigger a small DB lookup.
1892 * The title will also be lazy loaded, though setTitle() can be used to preload it.
1894 * @param IDatabase $db
1895 * @param int $pageId Page ID
1896 * @param int $revId Known current revision of this page
1897 * @return Revision|bool Returns false if missing
1900 public static function newKnownCurrent( IDatabase
$db, $pageId, $revId ) {
1901 $cache = ObjectCache
::getMainWANInstance();
1902 return $cache->getWithSetCallback(
1903 // Page/rev IDs passed in from DB to reflect history merges
1904 $cache->makeGlobalKey( 'revision', $db->getWikiID(), $pageId, $revId ),
1906 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
1907 $setOpts +
= Database
::getCacheSetOptions( $db );
1909 $rev = Revision
::loadFromPageId( $db, $pageId, $revId );
1910 // Reflect revision deletion and user renames
1912 $rev->mTitle
= null; // mutable; lazy-load
1913 $rev->mRefreshMutableFields
= true;
1916 return $rev ?
: false; // don't cache negatives
1922 * For cached revisions, make sure the user name and rev_deleted is up-to-date
1924 private function loadMutableFields() {
1925 if ( !$this->mRefreshMutableFields
) {
1926 return; // not needed
1929 $this->mRefreshMutableFields
= false;
1930 $dbr = wfGetLB( $this->mWiki
)->getConnectionRef( DB_REPLICA
, [], $this->mWiki
);
1931 $row = $dbr->selectRow(
1932 [ 'revision', 'user' ],
1933 [ 'rev_deleted', 'user_name' ],
1934 [ 'rev_id' => $this->mId
, 'user_id = rev_user' ],
1937 if ( $row ) { // update values
1938 $this->mDeleted
= (int)$row->rev_deleted
;
1939 $this->mUserText
= $row->user_name
;